﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Kwan.Data
{
    /// <summary>
    /// 数据连接句柄对象
    /// </summary>
    class ConnectionHandler : IDbTransaction, IDisposable
    {
        public ConnectionHandler(IDbConnection connection)
        {
            mConnection = connection;
        }
        private IDbConnection mConnection;
        public IDbConnection Connection
        {
            get
            {
                return mConnection;
            }


        }
        internal IDriver Driver
        {
            get;
            set;
        }
        private IDbTransaction mTransaction;
        public IDbTransaction Transaction
        {
            get
            {
                return mTransaction;

            }
        }
        public void BeginTransaction()
        {
            if (Transaction == null)
                mTransaction = Connection.BeginTransaction();
        }
        #region IDisposable 成员

        public void Dispose()
        {
            Rollback();
            if (mConnection != null)
                mConnection.Dispose();
        }

        #endregion
        #region IDbTransaction 成员

        public void Commit()
        {
            if (Transaction != null)
            {

                Transaction.Commit();
                mTransaction = null;
            }
        }

        public IsolationLevel IsolationLevel
        {
            get { return IsolationLevel.Unspecified; }
        }

        public void Rollback()
        {
            if (Transaction != null)
            {

                Transaction.Rollback();
                mTransaction = null;
            }
        }

        #endregion
        private void OnInitCommand(IDbCommand cmd)
        {
            cmd.Connection = Connection;
            if (Transaction != null)
                cmd.Transaction = Transaction;
            if(DBContext.Executing !=null)
            {
                DBContext.Executing(cmd);
            }
        }
        public int ExecuteNonQuery(Command cmd)
        {
            IDbCommand _execmd = cmd.CreateCommand(Driver);
            OnInitCommand(_execmd);
            return _execmd.ExecuteNonQuery();
        }
        public IDataReader ExecuteReader(Command cmd)
        {
            IDbCommand _execmd = cmd.CreateCommand(Driver);
            OnInitCommand(_execmd);
            return _execmd.ExecuteReader();
        }

        public object ExecuteScalar(Command cmd)
        {
            IDbCommand _execmd = cmd.CreateCommand(Driver);
            OnInitCommand(_execmd);
            return _execmd.ExecuteScalar();
        }

        public DataSet ExecuteDataSet(Command cmd)
        {
            IDbCommand _execmd = cmd.CreateCommand(Driver);
            OnInitCommand(_execmd);
            IDataAdapter da = Driver.DataAdapter(_execmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds;
        }
    }
    
}
